<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="__static/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="__static/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <link rel="stylesheet" href="__static/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }

        .layui-iconpicker-body.layui-iconpicker-body-page .hide {
            display: none;
        }

        .layui-iconpicker .layui-iconpicker-item {
            height: 36px;
        }

        .layui-iconpicker {
            margin-top: 10px;
        }

        .rules-box {
            border: 1px solid #e6e6e6;
            border-radius: 2px;
            padding: 10px;
        }
        .rules-box .layui-tree-iconClick {
            display: none;
        }
        .layui-tree .layui-form-checkbox {
            margin-left: 10px!important;
        }
    </style>
</head>

<body>
    <div class="layui-form layuimini-form layui-row">
        <input type="hidden" name="id" value="{$data.id|default='0'}">
        <div class="layui-col-xs12">
            <div class="layui-form-item">
                <labe class="layui-form-label required">角色名称</labe>
                <div class="layui-input-inline">
                    <!-- lay-verify="required"  -->
                    <input type="text" name="name" lay-reqtext="角色名称不能为空" placeholder="请输入角色名称" value="{$data.name|default=''}" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">角色描述</label>
                <div class="layui-input-block">
                    <input type="text" name="note" placeholder="角色描述信息" value="{$data.note|default=''}" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">角色权限</label>
                <div class="layui-input-block">
                    <div id="rules" class="rules-box"></div>
                    <input type="hidden" name="vals" value="{$data.tree_vals|default='[]'}">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">排序</label>
                <div class="layui-input-inline">
                    <input type="number" name="sort" placeholder="请输入排序" value="{$data.sort|default=0}" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
                </div>
            </div>
        </div>
    </div>

    <script src="__static/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
    <script src="__static/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
    <script>
        layui.use(['form', 'tree'], function() {
            var form = layui.form,
                layer = layui.layer,
                tree = layui.tree,
                $ = layui.$;
                
            var id = $('[name=id]').val();
            var tree_vals = '';

            tree_vals = JSON.parse($('input[name=vals]').val()) || [];
            tree_vals = tree_vals.map(function(a) {
                return parseInt(a);
            });

            $.get('/system/role_edit/p_tree/json', function(d) {
                tree.render({
                    elem: '#rules',
                    id: 'rules_tree',
                    data: d.data,
                    showCheckbox: true,
                    click: function(a) {
                    	
                        event.preventDefault();
                        event.stopPropagation();
                        return false;
                    }
	                
                });
                tree.setChecked('rules_tree', tree_vals);
            });

            //监听提交
            form.on('submit(saveBtn)', function(data) {
                var url = '/system/role_save';
                var rules_tree = tree.getChecked('rules_tree');
                var sel_ids = [];
                var tree_vals = [];
                map_tree(rules_tree, function(d) {
                    if (!d.children)
                        tree_vals.push(d.id);
                    sel_ids.push(d.id);
                });
                data.field.rules = sel_ids;
                data.field.tree_vals = tree_vals;
                layer.load(2);
                $.post(url, data.field, function(d) {
                    var index = layer.alert(d.msg, '', function() {
                        layer.close(index);
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    });
                    layer.closeAll('loading');
                });

                return false;
            });
        });

        function map_tree(arr, cb, key='children') {
        	//console.log(1);
            if (arr.constructor != Array)
                return;
            for (var i in arr) {
                cb(arr[i]);
                if (arr[i][key])
                    map_tree(arr[i][key], cb, key);
            }
        }
    </script>
</body>

</html>